#!/usr/bin/env python

class dotdict ( dict ):
	def get ( self, name, defval = None ):
		try:
			return self.__getitem__ ( name )
		except:
			return defval

	def __getitem__ ( self, name ):
		import sys

		#sys.stderr.write ( "\n\n\n---DOTDICT: %s\n" % name )

		keys = name.split ( "." )
		if len ( keys ) == 1:
			return super ( dotdict, self ).__getitem__ ( keys [ 0 ] )

		#sys.stderr.write ( "DOTDICT: %s\n" % keys [ 0 ] )
		dct = super ( dotdict, self ).__getitem__ ( keys [ 0 ] )

		for k in keys [ 1 : -1 ]:
			#sys.stderr.write ( "DOTDICT: %s\n" % k )
			dct = dct [ k ]

		#sys.stderr.write ( "DOTDICT ***: %s\n%s\n" % ( keys [ -1 ], dct [ keys [ -1 ] ] ) )
		return dct [ keys [ -1 ] ]


if __name__ == '__main__':
	"""
	import yaml
	cfg = yaml.load ( open ( 'liwe.test.yaml' ) )

	d = dotdict ( cfg )

	print d [ 'site.email.sender' ]
	print d.get ( "debug.value", -1 )
	"""

	a = { 
		"a" : {
			"b" : {
				"c" : 1
			}
		},

		"d" : {
			"e" : {
				"f" : 2
			}
		},

		"small" : {
			"value": 999
		},

		"hello" : "world"
	}

	d = dotdict ( a )	

 	print d [ "a.b.c" ]
 	print d [ "d.e.f" ]
	print d [ "small.value" ]

	print d [ "hello" ]

	print d.get ( "a.b.e" )
	print d.get ( "small.value" )
